//
//  MovieViewController.m
//  UI_元旦豆瓣半完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "DetailsViewController.h"
#import "MovieCollectionViewCell.h"

#define WIDCH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UIView *myView;


@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDCH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self navigation];
    [self dataHandler];
    [self creatCollection];
   
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDCH, HEIGHT)];

    
}
// CollectionView 初始化
- (void)creatCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((375 - 40) / 3, 200);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
}




- (void)tableViewAction:(UITabBarItem *)item {
    
    [_myView removeFromSuperview];
    
    UIImage *image = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
   
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction:)];
    self.navigationItem.rightBarButtonItem = item1;
    
    [self viewDidLoad];
    
    
}




- (void)collectionAction:(UIBarButtonItem *)item {
    
    [_tableView removeFromSuperview];
    
    [self creatCollection];
    
    UIImage *image = [UIImage imageNamed:@"btn_nav_list@2x.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(tableViewAction:)];
    self.navigationItem.rightBarButtonItem = item2;
    
    self.navigationController.navigationBar.translucent = NO;
    
    
    [_myView addSubview:_collection];
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection release];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    
    [_collection registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];
    
    [self.view addSubview:_myView];
    
}

- (void)navigation {
    self.navigationItem.title = @"电影";
    self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
    UIImage *image = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction:)];
    self.navigationItem.rightBarButtonItem = item;
}

// CollectionView delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    MovieModel *model = _modelArr[indexPath.row];
    [cell.movieImage sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.nameLable.text = model.original_title;
    
    
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MovieModel *model = _modelArr[indexPath.row];
    
    cell.titleLable.text = model.original_title;
    [cell.movieImage sd_setImageWithURL:[NSURL URLWithString:model.image]];
    
    return cell;
    
}
// tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//    NSLog(@"%@", result);
    NSDictionary *dic = result[@"result"][indexPath.row];
    
    DetailsViewController *deVC = [[DetailsViewController alloc] init];
    deVC.detail = dic;
    [self.navigationController pushViewController:deVC animated:YES];
    [deVC release];
    
    deVC.mArr = _arr;

    
  
}


// CollectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //    NSLog(@"%@", result);
    NSDictionary *dic = result[@"result"][indexPath.row];
    
    DetailsViewController *deVC = [[DetailsViewController alloc] init];
    deVC.detail = dic;
//    NSLog(@"💩💩💩💩💩💩💩%@", dic);
    [self.navigationController pushViewController:deVC animated:YES];
    [deVC release];
    
    deVC.mArr = _arr;

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 230;
}



// 数据处理
- (void)dataHandler {
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    //    NSLog(@"%@", arr);
    
    self.modelArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
    }
    
    self.arr = [NSMutableArray array];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
